
function main()

    global strings;
    global delta;
    
    thresh = 0.08;
    show = 0;

    persistent realbal;
    
    start = datenum(2011, 6, 1);
    stop  = datenum(2012, 6, 1);

    if ~exist('realbal') || isempty(realbal) || 1
        realbal = loadBallance('/Users/justaceclutter/Downloads/accountballance.xls');
    end
    models = getMoneyModels();
    deltas = cell(length(models),1);
    ballenceFromEpoch = cell(length(models), 1);
    ballenceFromEnd   = cell(length(models), 1);

    [realbal_X realbal_Y] = extractXYFromMap(realbal);
    
    for i = 1:length(models)
        [delta strings] = getModelDailyDelta(models{i}, start, stop);

        tmp = containers.Map(1,1); remove(tmp, 1);
        for j = start:stop
            if j > max(realbal_X)
                if isempty(tmp)
                    tmp(j) = 0;
                else
                    tmp(j) = tmp(j-1) + delta(j);
                end
            else
                tmp(j) = realbal(j);
            end
        end
        ballenceFromEnd{i} = tmp;

        epoch = models{i}.startdate;    
        if epoch == -1
            ballenceFromEpoch{i} = ballenceFromEnd{i};
        else
            tmp = containers.Map(1,1); remove(tmp, 1);
            for j = start:stop
                if j < epoch && j <= max(realbal_X)
                    tmp(j) = realbal(j);
                else
                    tmp(j) = tmp(j-1) + delta(j);
                end
            end
            ballenceFromEpoch{i} = tmp;
        end

        deltas{i} = delta;
    end

    plotIndex = 1;

    [realbal_X realbal_Y] = extractXYFromMap(realbal);

    figure;
    stairs(realbal_X, realbal_Y,'-r');
    xlabel('Date');
    ylabel('US Dollars');
    title('Real Checking Account Ballence', 'FontWeight', 'bold');
    set(gca, 'XLim', [min(realbal_X) max(realbal_X)]);
    datetick('keeplimits');

    res = cell(0);
    dres = cell(0);

    limits = [start,stop];

    fig = figure;
    %set(fig, 'PaperOrientation', 'Landscape');
    if show == 1
        set(fig, 'OuterPosition', [0 0 800 450]);
        axes1 = axes('Position', [0.1, 0.23, 0.4, 0.7]);
        axes2 = axes('Position', [0.1, 0.1, 0.4, 0.1]);
    else
        axes1 = axes('Position', [0.12, 0.23, 0.83, 0.7]);
        axes2 = axes('Position', [0.12, 0.1, 0.83, 0.1]);
    end

    axes(axes1);
    clear showbal;
    indexs = cellfun(@double, keys(realbal));
    showbal = containers.Map(1,1); remove(showbal, 1);
    for i = 1:length(indexs)
        if indexs(i) >= start && indexs(i) <= stop
            showbal(indexs(i)) = realbal(indexs(i));
        end
    end

    xlimits = [start stop];
    xlimits_w = diff(xlimits);
    xlimits_m = sum(xlimits)*0.5;
    xlimits = ([1 1]*xlimits_m) + ([-xlimits_w +xlimits_w]/(0.8*2));

    if exist('showbal') && ~isempty(showbal)

        [showbal_X showbal_Y] = extractXYFromMap(showbal);
        
        ylimits = [min(showbal_Y) max(showbal_Y)];
        ylimits_w = diff(ylimits);
        ylimits_m = sum(ylimits)*0.5;
        ylimits = ([1 1]*ylimits_m) + ([-ylimits_w +ylimits_w]/(0.8*2));

        stairs(showbal_X, showbal_Y, '-b', 'LineWidth', 3);
        set(gca, 'YLim', ylimits);
    end
    
    set(gca, 'XLim', xlimits);
    hold on;
    datetick('keeplimits');

    for i = 1:length(models)
        tmpline = ballenceFromEnd{i};
        indexs = cellfun(@double, keys(tmpline));
        tmpdata = containers.Map(1,1); remove(tmpdata, 1);
        
        for j = 1:length(indexs)
            if indexs(j) >= start && indexs(j) <= stop
                tmpdata(indexs(j)) = tmpline(indexs(j));
            end
        end

        [tmpdata_X tmpdata_Y] = extractXYFromMap(tmpdata);
        
        ylimits(2) = max(max(tmpdata_Y, ylimits(2)));
        ylimits(1) = min(min(tmpdata_Y, ylimits(1)));
        ylimits_w = diff(ylimits);
        ylimits_m = sum(ylimits)*0.5;
        ylimits = ([1 1]*ylimits_m) + ([-ylimits_w +ylimits_w]/(0.8*2));        
        
        stairs(tmpdata_X, tmpdata_Y, ':r', 'LineWidth', 1);
        hold on;

        set(gca, 'YLim', ylimits);
        
    end

    if 0
    for i = 1:length(am)
        showbal = zeros(0,2);
        for j = 1:size(res{i},1)
            if res{i}(j,1) >= starttime && res{i}(j,1) <= stoptime
                showbal = [showbal; res{i}(j,1), res{i}(j,2)];
            end
        end

        for m = 1:size(showbal,1)
            dat = showbal(m,1);
            [a b] = sort(abs(minvals(:,1)-dat));
            index = b(1);
            if minvals(index,2) > showbal(m,2)
                minvals(index,2) = showbal(m,2);
            end
            if maxvals(index,2) < showbal(m,2)
                maxvals(index,2) = showbal(m,2);
            end
        end

        tmax = max(showbal(:,2));
        tmin = min(showbal(:,2));
        limits(1) = min(limits(1), tmin);
        limits(2) = max(limits(2), tmax);
        gtype = amgraphics{i};
        stairs(showbal(:,1), showbal(:,2),gtype);    
        hold on;
    end

    limitw = limits(2)-limits(1);
    limitm = 0.5*(limits(1) + limits(2));
    limits(1) = limitm - 0.5*(limitw/0.8);
    limits(2) = limitm + 0.5*(limitw/0.8);

    set(gca, 'XTick', []);
    set(gca, 'XLim', [starttime stoptime]);
    set(gca, 'YLim', limits);
    ylabel('Bank Ballance (US Dollars)');
    title('Projected Checking Account Ballance', 'FontWeight', 'bold');


    if show == 1
        five = diff(get(gca, 'YLim'))*0.05;

        showindex = 1;
        thealerts = alerts{showindex};
        for j = 2:size(thealerts,1)
            fprintf('showing an alert\n');
            tday = thealerts{j,2};
            [a b] = sort(abs(maxvals(:,1)-tday));
            yval = maxvals(b(1),2) + five;
            xr = diff(get(gca, 'XLim'))*0.03;
            yr = diff(get(gca, 'YLim'))*0.03;
            xs = diff(get(gca, 'XLim'));
            ys = diff(get(gca, 'YLim'));
            annotation('ellipse', [tday/xs yval/ys 0.05 0.05])
            [x y] = ellipsoid(tday, yval, 0, xr,yr,0);
    %        plot(x,y,'b');
            text(tday, yval, sprintf('%i', thealerts{j,1}), 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Middle');
        end
    end

    axes(axes2);
    limits = [999999 0];
    for i = 1:length(am)
        showbal = zeros(0,2);
        for j = 1:size(dres{i},1)
            if dres{i}(j,1) >= starttime && dres{i}(j,1) <= stoptime
                showbal = [showbal; dres{i}(j,1), dres{i}(j,2)];
            end
        end

        tmax = max(showbal(:,2));
        tmin = min(showbal(:,2));
        limits(1) = min(limits(1), tmin);
        limits(2) = max(limits(2), tmax);
        gtype = amgraphics{i};
        stairs(showbal(:,1), showbal(:,2),gtype);
        hold on;
    end

    limitw = limits(2)-limits(1);
    limitm = 0.5*(limits(1) + limits(2));
    limits(1) = limitm - 0.5*(limitw/0.8);
    limits(2) = limitm + 0.5*(limitw/0.8);


    set(gca, 'XLim', [starttime stoptime]);
    set(gca, 'YLim', limits);
    set(gca, 'XTick', starttime:2:stoptime);
    datetick('x',6, 'keeplimits', 'keepticks');
    xlabel('Date');
    ylabel('Differnece');

    fp = fillPage(gcf, 'margins', [0 0 0 0], 'papersize', [11 8.5]);
    print -painters -dpdf -r600 '/Users/justaceclutter/Desktop/TripProjection.pdf';
    set(gcf, fp);
    end
end

